package cn.itcast.bos.service.base.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.base.CourierDao;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.CourierService;

@Service
@Transactional
public class CourierServiceImpl implements CourierService {
	@Autowired
	private CourierDao courierDao;

	@Override
	public void save(Courier courier) {
		courierDao.save(courier);
	}
	/**
	 *根据条件并分页查询
	 */
	@Override
	public Page<Courier> findAll(Courier model, Pageable pageable) {
		final String courierNum = model.getCourierNum();
		final String company = model.getCompany();
		final String type = model.getType();
		final Standard standard = model.getStandard();
		final  String name = model.getName();
		final String telephone = model.getTelephone();
		//Specification对象封装查询条件
		Specification<Courier> spec=new Specification<Courier>() {
			//root查询的根对象，相当与表名
			//CriteriaBuilder工厂对象，创建Predicate对象
			//Predicate相当于查询条件
			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// TODO Auto-generated method stub
				List<Predicate> list=new ArrayList<>();
				if(StringUtils.isNotBlank(name)){
					Predicate p5 = cb.like(root.get("name").as(String.class),"%"+name+"%");
					list.add(p5);
				}
				if(StringUtils.isNotBlank(telephone)){
					Predicate p6 = cb.like(root.get("telephone").as(String.class),"%"+telephone+"%");
					list.add(p6);
				}

				if(StringUtils.isNotBlank(courierNum)){
					Predicate p1 = cb.equal(root.get("courierNum").as(String.class), courierNum);
					list.add(p1);
				}
				if(StringUtils.isNotBlank(company)){
					Predicate p2 = cb.equal(root.get("company").as(String.class), company);
					list.add(p2);
				}
				if(StringUtils.isNotBlank(type)){
					Predicate p3 = cb.equal(root.get("type").as(String.class),type);
					list.add(p3);
				}
				//快递员收派标准需要内关联查询
				 if(standard!=null&&StringUtils.isNotBlank(standard.getName())){
					 Join<Object, Object> join = root.join("standard",JoinType.INNER);
					 Predicate p4 = cb.equal(join.get("name").as(String.class), standard.getName());
					 list.add(p4);
				 }
				 if(list!=null&&list.size()==0){
					 return null;
				 }
				 Predicate[] params=new Predicate[list.size()];
				 params= list.toArray(params);
				return  cb.or(params);
			}
		};
		return courierDao.findAll(spec,pageable);
	}
	/**
	 * 批量删除
	 * 逻辑删除，不直接进行删除，只是进行状态的更新
	 */
	@RequiresPermissions("courier_delete")
	@Override
	public void deleteBantch(String ids) {
			if(StringUtils.isNotBlank(ids)){
				String[] strs = ids.split(",");
				for (String id : strs) {
					//快递员的逻辑删除
					courierDao.logicDelete(Integer.parseInt(id));
					//删除快递员和定区之间的关系
					Courier courier = courierDao.findOne(Integer.parseInt(id));
					Set<FixedArea> fixedAreas = courier.getFixedAreas();
					for (FixedArea fixedArea : fixedAreas) {
						fixedArea.getCouriers().remove(courier);
					}
				}
			}
	}
	@Override
	public List<Courier> findNotDeleteAndNotLinked(String fixedAreaId) {
		return courierDao.findNotDeleteAndNotAssocation(fixedAreaId);
	}
	
	/**
	 * 快递员的还原
	 */
	public void restore(String ids) {
		if(StringUtils.isNotBlank(ids)){
			String[] strs = ids.split(",");
			for (String id : strs) {
				courierDao.restore(Integer.parseInt(id));
			}
		}
	}

}
